package org.mine.design.decorator.service;


import lombok.extern.slf4j.Slf4j;
import org.mine.design.decorator.data.RiderOrder;
import org.mine.design.decorator.data.RiderOrderPool;
import org.mine.design.decorator.enums.RiderOrderState;

import java.util.List;



@Slf4j
public abstract class OrderDeliveryAbstractService implements OrderDeliveryService {

    /**
     * 接单
     */
    @Override
    public void accept(RiderOrder riderOrder) {
        log.info("订单接单...");

        changeOrderState(RiderOrderState.ACCEPT, riderOrder.getOrderNo());
    }

    /**
     * 配送
     */
    @Override
    public void delivery(RiderOrder riderOrder) {
        log.info("订单配送中...");

        changeOrderState(RiderOrderState.DELIVERY, riderOrder.getOrderNo());
    }


    /**
     * 完成
     */
    @Override
    public void complete(RiderOrder riderOrder) {
        log.info("订单配送完成...");

        changeOrderState(RiderOrderState.COMPLETE, riderOrder.getOrderNo());
    }


    private void changeOrderState(RiderOrderState state, String riderOrderNo) {
        List<RiderOrder> pool = RiderOrderPool.getPool();
        pool.stream()
                .filter(order -> order.getOrderNo().equals(riderOrderNo))
                .findFirst().ifPresent(
                        order -> {
                            order.setState(state);
                        }
                );
    }

}
